package com.campus.controller;

import com.alibaba.excel.EasyExcel;
import com.campus.common.Result;
import com.campus.common.StudentBatchAddListener;
import com.campus.common.StudentBatchExamineListener;
import com.campus.domain.Dto.StudentBatchAddDto;
import com.campus.domain.Dto.StudentCreateDto;
import com.campus.domain.Vo.StudentExamineBatchVo;
import com.campus.service.IStudentService;
import com.sun.deploy.net.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
@Slf4j
public class StudentController {

    @Autowired
    private IStudentService studentService;

    @PostMapping("/add")
    public Result addStudent(@RequestBody @Validated StudentCreateDto studentCreateDto){
        int rs = studentService.addStudent(studentCreateDto);

        return rs == 1 ? Result.success() : Result.error();
    }

    @PutMapping("/examine/{id}")
    public Result examineStudent(@PathVariable Long id){
        int rs = studentService.examineStudent(id);

        return rs == 1 ? Result.success() : Result.error();
    }

    @PostMapping("/addBatch")
    @ResponseBody
    public Result addBatch(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), StudentBatchAddDto.class, new StudentBatchAddListener(studentService)).sheet().doRead();
            return Result.success("文件上传成功");
        } catch (IOException e) {
            return Result.error("文件上传失败");
        }
    }

    @PostMapping("/export/template")
    public void uploadTemplate(HttpServletResponse response) {

        List<StudentBatchAddDto> list = new ArrayList<>();
        try{
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("content-Disposition","attachment;filename="+ URLEncoder.encode("测试模板","UTF-8")+".xlsx");
            EasyExcel.write(response.getOutputStream(),StudentBatchAddDto.class).sheet().doWrite(list);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @PostMapping("/export/examine")
    public void uploadExamine(HttpServletResponse response) {

        List<StudentExamineBatchVo> list = studentService.getStudentForExamine();
        try{
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("content-Disposition","attachment;filename="+ URLEncoder.encode("审核学生表","UTF-8")+".xlsx");
            EasyExcel.write(response.getOutputStream(),StudentExamineBatchVo.class).sheet().doWrite(list);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @PostMapping("/examineBatch")
    @ResponseBody
    public Result examineBatch(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), StudentExamineBatchVo.class, new StudentBatchExamineListener(studentService)).sheet().doRead();
            return Result.success("文件上传成功");
        } catch (IOException e) {
            return Result.error("文件上传失败");
        }
    }

}
